package cn.est.service.impl;

import cn.est.constants.Constants;
import cn.est.dto.BrandDto;
import cn.est.dto.ClassifyDto;
import cn.est.dto.ModelDto;
import cn.est.pojo.Brand;
import cn.est.pojo.BrandClassifyRelation;
import cn.est.pojo.Classify;
import cn.est.pojo.Model;
import cn.est.redis.RedisKeyUtils;
import cn.est.redis.RedisUtils;
import cn.est.resp.ResultUtils;
import cn.est.service.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ClassifyWebServiceImpl implements ClassifyWebService {
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private BrandClassifyRelationService bcrService;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public List<ClassifyDto> classifyTree() throws Exception {
        //编写调用查询业务层对应的方法，组装数据
        //性能优化
        //获取redis是否存储商品分类的信息
        String key= RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.KEY_HOME_TREE);
        Object obj=redisUtils.get(key);
        //调用业务商品分类的查询方法
        List<Classify> classifyList=classifyService.getClassifyListByMap(null);
        List<ClassifyDto> classifyDtoList=new ArrayList<ClassifyDto>();
        if(obj==null){
            List<Brand> brandList=brandService.getBrandListByMap(null);
            for (Classify c:classifyList) {
                ClassifyDto classifyDto=new ClassifyDto();
                //BeanUtils:对实体对象操作的工具类
                //copyProperties(原来的对象,新的对象)：复制属性并赋值
                BeanUtils.copyProperties(c,classifyDto);//

                //给brandList集合赋值
                //手机    ——  品牌：某个分类的品牌
                Map cParam= new HashMap();
                cParam.put("classifyId",c.getId());//
                List<BrandClassifyRelation> bcrList=bcrService.getBrandClassifyRelationListByMap(cParam);
                List<BrandDto> brandDtoList=new ArrayList<BrandDto>();
                //组装数据   brandDtoList
                for(BrandClassifyRelation bcr:bcrList){
                    for(Brand b:brandList){
                        if(bcr.getBrandId()==b.getId()){
                            BrandDto brandDto=new BrandDto();
                            BeanUtils.copyProperties(b,brandDto);//拷贝属性
                            //给品牌的型号列表/类型 (modelList)
                            List<ModelDto> modelDtoList=new ArrayList<ModelDto>();
                            Map mParam=new HashMap();
                            mParam.put("brandId",b.getId());
                            mParam.put("classifyId",bcr.getClassifyId());
                            List<Model> modelList=modelService.getModelListByMap(mParam);
                            //组装数据
                            for(Model m:modelList){
                                ModelDto modelDto=new ModelDto();
                                BeanUtils.copyProperties(m,modelDto);
                                modelDtoList.add(modelDto);
                            }
                            brandDto.setModelList(modelDtoList);//给品牌的modelList赋值
                            brandDtoList.add(brandDto);
                        }
                    }

                }
                classifyDto.setBrandList(brandDtoList);//给brandList集合赋值
                classifyDtoList.add(classifyDto);//把classifyDto添加到classifyDtoList集合
            }
            //将商品分类信息存储在redis中
            redisUtils.putValue(key,JSONObject.toJSONString(classifyDtoList));
        }else{
            classifyDtoList= JSONObject.parseArray(redisUtils.getValue(key),ClassifyDto.class);
        }

        return classifyDtoList;
    }
}
